package py.com.alfa.escriba.gui.exception;

import java.io.IOException;

import javax.ws.rs.ext.Provider;

import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;

@Provider
public class ServiceException extends IOException {
	private static final long serialVersionUID = -1417575380801996290L;
	private ErrorCode errorCode;
//	private Locale locale;
//	private static final String DEFAULT_MESSAGE = "HA OCURRIDO UN ERROR";
	
	public ServiceException(ErrorCode errorCode) {
		super();
		this.errorCode = errorCode;
	}
	
	public ServiceException(ErrorCode errorCode, String message, Throwable cause) {
		super(message, cause);
		this.errorCode = errorCode;
	}
	
	public ServiceException(ErrorCode errorCode, String message) {
		super(message);
		this.errorCode = errorCode;
	}
	
	public ServiceException(ErrorCode errorCode, Throwable cause) {
		super(cause);
		this.errorCode = errorCode;
	}
	
	public ServiceException(ErrorCode errorCode, Throwable cause, String message, Object...args) {
		super(message, cause);
		this.errorCode = errorCode;
	}
	
	public ServiceException(ErrorCode errorCode, String message, Object...args) {
		super(message);
		this.errorCode = errorCode;
	}

	public ErrorCode getErrorCode() {
		return this.errorCode;
	}
  
	public String getErrorMessage() {
		StringBuilder sb = new StringBuilder();
		sb.append(getErrorCode()).append("=").append(this.getMessage())
				.append("\n");
		return sb.toString();
	}
  
	@JsonSerialize(include=Inclusion.NON_EMPTY)
	static final class JSONError {
		private String errorKey;
		private String errorMessage;		
		private String causeString;
		private String messages[];
		
		public String getErrorKey() {
			return errorKey;
		}
		public void setErrorKey(String errorKey) {
			this.errorKey = errorKey;
		}
		public String getErrorMessage() {
			return errorMessage;
		}
		public void setErrorMessage(String errorMessage) {
			this.errorMessage = errorMessage;
		}
		public String getCauseString() {
			return causeString;
		}
		public void setCauseString(String causeString) {
			this.causeString = causeString;
		}
		public String[] getMessages() {
			return messages;
		}
		public void setMessages(String[] messages) {
			this.messages = messages;
		}		
	}
}
